package org.sample.programs;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class ThreadingActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startFirstThread();
    }
    
    private static final int THREAD_ONE_COMPLETE = 0;
    private static final int THREAD_TWO_COMPLETE = 1;
    private final String audiofolderPath = "/sdcard/Songs/";
    private final String imagefolderPath = "/sdcard/Images/";

    private void startFirstThread()
    {
        Thread backgroundThread1 = new Thread()
        {
            @Override
            public void run() 
            {
            	System.out.println("inside startFirstThread()");
            	
            	// do first step
            	String audioFilePath = audiofolderPath+"india.mp3";
    	    	
    	    	Intent intent = new Intent(); 
	            intent.setAction(android.content.Intent.ACTION_VIEW);  
	            File file = new File(audioFilePath);  
	            intent.setDataAndType(Uri.fromFile(file), "audio/*");  	          
	            startActivity(intent);
            	
            	// finished first step
            	System.out.println("finished startFirstThread()");     
                
                Message msg = Message.obtain();
                msg.what = THREAD_ONE_COMPLETE;
                handler.sendMessage(msg);
            }
        };
        backgroundThread1.start();
    }
    private void startSecondThread()
    {
        Thread backgroundThread2 = new Thread()
        {
            @Override
            public void run() 
            {
            	System.out.println("inside startSecondThread()");
                // do second step
            	String imageFilePath = imagefolderPath+"Tiger.jpg";
    	    	
    	    	Intent intent = new Intent(); 
	            intent.setAction(android.content.Intent.ACTION_VIEW);  
	            File file = new File(imageFilePath);  
	            intent.setDataAndType(Uri.fromFile(file), "image/*");  	          
	            startActivity(intent);
            	
            	// finished second step
            	System.out.println("finished startSecondThread()");
               
                Message msg = Message.obtain();
                msg.what = THREAD_TWO_COMPLETE;
                handler.sendMessage(msg);
            }
        };
        backgroundThread2.start();
    }
    
    private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) 
        {
        	System.out.println("************************************");
            switch(msg.what)
            {
            case THREAD_ONE_COMPLETE:
            	// Call second thread after first thread completes;
            	System.out.println("Thread One Completed");
            	startSecondThread();
                break;
            case THREAD_TWO_COMPLETE:
                // do final steps;
            	System.out.println("Thread two Completed");
                break;
            }
        }
    };
}